package top.maof.book.spider.develop.contents.wanshulou;

import lombok.Data;
import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.spider.develop.Split;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 万书楼
 */
@Data
@TargetUrl("https://www.wshulou.com/\\d+/\\d+/")
@HelpUrl({"https://www.wshulou.com/paihang/", "https://www.wshulou.com/wanben/", "https://www.wshulou.com/top/[\\w\\?=/&.]*",
        "https://www.wshulou.com/xuanhuan/\\d+.html", "https://www.wshulou.com/dushi/\\d+.html", "https://www.wshulou.com/xianxia/\\d+.html",
        "https://www.wshulou.com/junshi/\\d+.html", "https://www.wshulou.com/yanqing/\\d+.html", "https://www.wshulou.com/wangyou/\\d+.html",
        "https://www.wshulou.com/kehuan/\\d+.html", "https://www.wshulou.com/qita/\\d+.html", "https://www.wshulou.com/\\d+/\\d+.html"})
public class Wanshulou implements AfterExtractor, Split {

    @ExtractBy(value = "//div[@class=bookinfo]/h1[@class=booktitle]/text()")
    private String name;

    @ExtractBy(value = "//div[@class=bookinfo]/p[@class=booktag]/a[@class=red]/text()")
    private String author;

    @ExtractBy(value = "//div[@class=book]/div[@class=bookcover]/img/@src")
    private String img;

    @ExtractBy("//ol[@class=breadcrumb]/li[2]/a/text()")
    private String classify;

    private Date updateDate;

    private Date insertDate = new Date();

    @ExtractBy(value = "//div[@class=bookinfo]/p[@class=bookintro]/text()")
    private String synopsis;

    @ExtractBy("//div[@class=bookinfo]/p[@class=booktag]/span[@class=red]/text()")
    private String state;

    private String url;

    private String content;

    private Integer webId = 5;

    public static final String CHARSET = "utf-8";

    @Override
    public Book getBook() {
        Book book = new Book();
        book.setAuthor(this.author.trim());
        book.setImg(this.img);
        book.setName(this.name.trim());
        book.setInsertDate(this.insertDate);
        book.setClassify(this.classify);
        book.setSynopsis(this.synopsis);
        book.setUpdateDate(this.updateDate);
        book.setState(this.state);
        return book;
    }

    @Override
    public Contents getContents() {
        Contents contents = new Contents();
        contents.setUpdateDate(this.updateDate);
        contents.setUrl(this.url);
        contents.setContent(this.content);
        contents.setWebId(this.webId);
        return contents;
    }


    @Override
    public void afterProcess(Page page) {
        //获取最新章节更新时间   更新时间：2020-07-08 20:24
        String updateStr = page.getHtml().xpath("//div[@class=bookinfo]/p[@class=booktime]/text()").toString().substring(5, 15).trim();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            this.updateDate = simpleDateFormat.parse(updateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }


        //img本身应该没问题  以防万一,还是需要确认无误
        if (!this.img.startsWith("http")) {
            if (this.img.startsWith("/"))
                this.img = "https://www.wshulou.com" + this.img;
            else {
                this.img = page.getUrl().toString().substring(0, page.getUrl().toString().lastIndexOf("/") + 1) + this.img;
            }
        }


        this.url = page.getUrl().toString();
    }

}

